package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: tanhua
 * @description: 用户信息
 * @author: leiyukun
 * @create: 2022-03-24 14:50
 **/
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private SettingsService settingsService;
    @Autowired
    private MovementsService movementsService;

    /**
     * 查询用户资料
     * 参数：token
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if (userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * /phone/sendVerificationCode-->post
     * /phone/checkVerificationCode-->post
     * /phone-->post
     * 更改手机号
     * 参数： token，verificationCode 验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        userService.sendMsg(settingsService.settings().getPhone());
        return ResponseEntity.ok(null);
    }

    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        String code = (String) map.get("verificationCode");
        String phone = settingsService.settings().getPhone();
        boolean verification = userInfoService.Verification(phone, code);
        Map<String, Boolean> map1 = new HashMap<>();
        map1.put("verification", verification);
        return ResponseEntity.ok(map1);
    }

    @PostMapping("/phone")
    public ResponseEntity SavePhone(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        settingsService.updatephone(phone);
        return ResponseEntity.ok(null);
    }

    /**
     * 用户更新头像
     *
     * @param headPhoto
     * @return
     */
    @PostMapping("/header")
    public ResponseEntity updateHeader(@RequestBody MultipartFile headPhoto) throws IOException {
        userInfoService.updateHead(headPhoto, UserHolder.getUserId());
        return ResponseEntity.ok(null);
    }

    /**
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        Map<String, Integer> map = userInfoService.findcounts();
        return ResponseEntity.ok(map);
    }

    /**
     *  互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * @param page
     * @param pagesize
     * @param type
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity type(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pagesize,
                               @RequestParam(defaultValue = "10") String nickname,
                               @PathVariable("type") Integer type) {
        PageResult pr =userInfoService.findByType(page,pagesize,type);
        List<VisitorsVo> list = movementsService.queryVisitorsList();
//        PageResult pr = new PageResult(5, 5, 0L, list);
        return ResponseEntity.ok(pr);
    }

}
